const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');

console.log('🐳 开始Docker构建和上传流程...');

// 配置
const config = {
  imageName: 'bot-server',
  tag: 'latest',
  registry: process.env.DOCKER_REGISTRY || '', // 例如: your-registry.com/your-namespace
  pushToRegistry: process.env.PUSH_TO_REGISTRY === 'true'
};

try {
  // 1. 优化构建
  console.log('📦 执行优化构建...');
  execSync('npm run build:optimized', { stdio: 'inherit' });

  // 2. 检查Docker是否安装
  try {
    execSync('docker --version', { stdio: 'pipe' });
  } catch (error) {
    console.error('❌ Docker未安装或未启动，请先安装Docker Desktop');
    process.exit(1);
  }

  // 3. 构建Docker镜像
  console.log('🔨 构建Docker镜像...');
  const imageTag = config.registry ? `${config.registry}/${config.imageName}:${config.tag}` : `${config.imageName}:${config.tag}`;
  execSync(`docker build -t ${imageTag} .`, { stdio: 'inherit' });

  // 4. 显示镜像信息
  console.log('📊 镜像信息:');
  execSync(`docker images ${imageTag}`, { stdio: 'inherit' });

  console.log('✅ Docker构建完成！');
  console.log(`📋 镜像标签: ${imageTag}`);
  console.log('🚀 运行命令:');
  console.log(`   docker run -p 3000:3000 -p 3001:3001 ${imageTag}`);

} catch (error) {
  console.error('❌ Docker构建失败:', error.message);
  process.exit(1);
} 